package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 就诊人表 服务实现类
 * </p>
 *
 * @author zhuo
 * @since 2022-03-16
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;

    //修改的时候根据id查询
    @Override
    public Patient getPatientId(Long id) {
        Patient patient = baseMapper.selectById(id);
        return this.packPatient(patient);
    }

    //获取就诊人列表
    @Override
    public List<Patient> findAll(Long userId) {
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Patient> patients = baseMapper.selectList(wrapper);

        // 进行数据填充
        patients.stream().forEach(item -> {
            this.packPatient(item);
        });
        return patients;
    }

    public Patient packPatient(Patient patient) {
        //省code
        String provinceCode = patient.getProvinceCode();
        //市code
        String cityCode = patient.getCityCode();
        //区code
        String districtCode = patient.getDistrictCode();

        // 证件类型编码获取证件类型名字
        dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        String provinceName = dictFeignClient.getName(provinceCode);
        String cityName = dictFeignClient.getName(cityCode);
        String districtName = dictFeignClient.getName(districtCode);

        Map<String, Object> param = patient.getParam();
        param.put("provinceString", provinceName);
        param.put("cityString", cityName);
        param.put("districtString", districtName);
        param.put("fullAddress", provinceName + cityName + districtName + patient.getAddress());

        return patient;
    }
}
